package mlp.game.scootaloo;

import com.hlidskialf.android.preference.SeekBarPreference;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;

public class SettingsActivity extends PreferenceActivity {
	private SharedPreferences sharedPreferences;
//	private Editor editor;

	@Override
	protected void onDestroy() {
//		editor.commit();
		super.onDestroy();
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getPreferenceManager().setSharedPreferencesName(GameActivity.SETTINGS);
        addPreferencesFromResource(R.xml.preferences);
        
        sharedPreferences = getPreferenceManager().getSharedPreferences();
//        editor = sharedPreferences.edit();
        
        if(sharedPreferences.getString("controllSheme", null) == null){
        	((ListPreference)findPreference("controllSheme")).setValueIndex(getDefaultControllSheme(getApplicationContext()));
        }
        
        if(Integer.valueOf(((ListPreference)findPreference("controllSheme")).getValue()) == ScootalooEngine.CONTROLLSHEME_SCREEN){
        	((SeekBarPreference)findPreference("helpAlpha")).setEnabled(true);
        } else {
        	((SeekBarPreference)findPreference("helpAlpha")).setEnabled(false);
        }
        
        ((ListPreference)findPreference("controllSheme")).setOnPreferenceChangeListener(new OnPreferenceChangeListener() {			
			@Override
			public boolean onPreferenceChange(Preference preference, Object newValue) {
				int value = Integer.valueOf((String) newValue);
				if(value == ScootalooEngine.CONTROLLSHEME_SCREEN){
					((SeekBarPreference)findPreference("helpAlpha")).setEnabled(true);
				} else {
					((SeekBarPreference)findPreference("helpAlpha")).setEnabled(false);					
				}
				return true;
			}
		});
	}
	
	public static int getDefaultControllSheme(Context ctx){
		Configuration c = ctx.getResources().getConfiguration();
		if(c.navigation == Configuration.NAVIGATION_DPAD || c.navigation == Configuration.NAVIGATION_TRACKBALL){
			return ScootalooEngine.CONTROLLSHEME_DPAD;			
		}
		return ScootalooEngine.CONTROLLSHEME_SCREEN;	
	}
	
}
